{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "------------------\n",
    "* 时间：2022/11/21/week14\n",
    "* 内容：函数与模块\n",
    "------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周目标\n",
    ">1. 函数的定义\n",
    "\n",
    ">2. 如何使用函数\n",
    "\n",
    ">3. 函数的返回值\n",
    "\n",
    ">4. 使用注释改进文档\n",
    "\n",
    ">5. 传递任意数量的实参\n",
    "\n",
    ">6. 函数与模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.函数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user():          #def(定义)\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "greet_user()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello \"+username+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello Tom~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"Tom\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    \"\"\"y=2x的函数计算\"\"\"\n",
    "    print(2*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "f(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 编程思维（计算思维）下，函数应用\n",
    "def nfu_news_url(类别,page):\n",
    "    for page in range(page):\n",
    "        print(\"https://www.nfu.edu.cn/\" + 类别 + \"/index\" + str(page) + \".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/xydt/index0.htm\n",
      "https://www.nfu.edu.cn/xydt/index1.htm\n",
      "https://www.nfu.edu.cn/xydt/index2.htm\n",
      "https://www.nfu.edu.cn/xydt/index3.htm\n",
      "https://www.nfu.edu.cn/xydt/index4.htm\n",
      "https://www.nfu.edu.cn/xydt/index5.htm\n",
      "https://www.nfu.edu.cn/xydt/index6.htm\n",
      "https://www.nfu.edu.cn/xydt/index7.htm\n",
      "https://www.nfu.edu.cn/xydt/index8.htm\n",
      "https://www.nfu.edu.cn/xydt/index9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"xydt\",10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/tzgg/index0.htm\n",
      "https://www.nfu.edu.cn/tzgg/index1.htm\n",
      "https://www.nfu.edu.cn/tzgg/index2.htm\n",
      "https://www.nfu.edu.cn/tzgg/index3.htm\n",
      "https://www.nfu.edu.cn/tzgg/index4.htm\n",
      "https://www.nfu.edu.cn/tzgg/index5.htm\n",
      "https://www.nfu.edu.cn/tzgg/index6.htm\n",
      "https://www.nfu.edu.cn/tzgg/index7.htm\n",
      "https://www.nfu.edu.cn/tzgg/index8.htm\n",
      "https://www.nfu.edu.cn/tzgg/index9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"tzgg\",10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.如何使用函数（传递参数）\n",
    ">1. positional argument（位置参数）\n",
    ">2. keyword argument（关键数参数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过案例来思考位置参数 关键字参数\n",
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-14-6586f0fcad5d>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-14-6586f0fcad5d>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "f(3,2,5)   # 位置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "f(y=10,x=3,z=7)   # 关键字参数，关键字参数不考虑顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9)    # 关键字参数在位置参数后面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=9)   # 关键字参数在位置参数后面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-23-ad077992a921>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-23-ad077992a921>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    f(3,y=9,10)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(3,y=9,10)   # 报错原因：关键字参数在位置参数前面 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "> 关键字参数不能放在位置参数前面"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">3. 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n",
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息,如果你的宠物是dog，可以不选择animal_type\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "    \n",
    "    \n",
    "describe_pet('harry','hamster')\n",
    "describe_pet('python','cat')\n",
    "describe_pet('jerry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function describe_pet in module __main__:\n",
      "\n",
      "describe_pet(pet_name, animal_type='dog')\n",
      "    显示宠物信息,如果你的宠物是dog，可以不选择animal_type\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",'world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world \n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print('hello','world',\"\")\n",
    "print('hello','world')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.函数的返回值\n",
    ">1. 几乎所有编程语言，函数是要求必须要 **写**返回值！\n",
    ">2. Python作为高级语言，可以 **不写** 返回值，但是一般情况下，也要写，不写的话，默认返回none\n",
    ">3. **函数的返回值**：return\n",
    ">>1. return可以返回过程运算的结果\n",
    ">>2. return 可以返回过程自定义的结果（由程序开发人员/设计人员定制的）\n",
    "## 4.函数的优势\n",
    "* 1. 可重复使用\n",
    "* 2. 具有强大的可扩展性\n",
    "* 3. 具有可定制化性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = f(2)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
